﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RLPUR.Common;
using RLPUR.Models;

namespace RLPUR.Web
{
    public partial class PayInvoiceEdit : LocalDialog
    {
        #region 属性

        /// <summary>
        /// 当前ID
        /// </summary>
        private string CurrentID
        {
            get
            {
                object tempObject = ViewState["CurrentID"];
                return (tempObject != null) ? (string)tempObject : string.Empty;
            }
            set
            {
                ViewState["CurrentID"] = value;
            }
        }

        #endregion

        protected override string QueryStringValidationPattern
        {
            get
            {
                return string.Empty; //关闭非法参数过滤
            }
        }

        /// <summary>
        /// 页面加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                #region 验证权限

                #endregion

                #region 获取参数

                string id = Request.QueryString["ID"];
                if (id != null && id.Trim().Length != 0)
                {
                    this.CurrentID = id.Trim();
                }

                #endregion

                //初始化
                this.Initialize();
            }

            #region 页面标题

            this.Title = PageTitle.Text;

            #endregion

            #region 页面要素

            this.AssignDialogCallback(CancelButton, "'Close'", "window");

            #endregion
        }

        #region 初始化

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            #region 页面内容


            #endregion

            if (this.CurrentID.Length == 0)
            {
                //新增
            }
            else
            {
                //编辑
                this.BindItem();
            }
        }

        #endregion

        #region 绑定数据

        /// <summary>
        /// 绑定项目
        /// </summary>
        private void BindItem()
        {
            using (PurProvider purProvider = new PurProvider())
            {
                //DataRow row = rtProvider.GetItemDetail(this.CurrentID);
                //if (row != null)
                //{
                //    TotalAmt.Text = row["improd"].ToString();
                //    KpNo.Text = row["imdes"].ToString();
                //    KpAmt.Text = row["imname"].ToString();
                //    KpDate.Text = row["imfac"].ToString().Trim();
                //    FinalDate.Text = row["immode"].ToString();
                //}
            }
        }

        #endregion

        #region 操作

        /// <summary>
        /// 确定
        /// </summary>
        protected void OKButton_Click(object sender, EventArgs e)
        {
            using (PurProvider purProvider = new PurProvider())
            {
                decimal totalAmt = Util.ToDecimal(TotalAmt.Text.Trim());
                decimal kpAmt = Util.ToDecimal(KpAmt.Text.Trim());
                if (totalAmt == 0 || kpAmt == 0)
                {
                    this.ShowWarningMessage("金额不正确");
                    return;
                }

                if (this.CurrentID.Length == 0)
                {
                    //新增
                    //try
                    //{
                    //    DataRow row = rtProvider.GetItemDetail(improd.Text.Trim());
                    //    if (row != null)
                    //    {
                    //        this.ShowErrorMessage("料号重复!请重新输入");
                    //        return;
                    //    }

                    //    rtProvider.InsertItemInfo(improd.Text.Trim(), imdes.Text.Trim(), imname.Text.Trim(), imfac.SelectedValue.Trim(), immode.Text.Trim(), imvnam.Text.Trim(), imium.Text.Trim(), impum.SelectedValue.Trim(), cvr, imtyp.SelectedValue.Trim(), ukg, usiz, max, min, sct, nct, nct, imabc.SelectedValue.Trim(), imbpc.Text.Trim(), imkpc.Text.Trim(), ltm, "0", DateTime.Now, DateTime.Now, LocalGlobal.CurrentUserID);
                    //}
                    //catch (Exception error)
                    //{
                    //    this.ShowErrorMessage(this.GetGlobalResourceString("CreateErrorMessage") + error.Message);
                    //    return;
                    //}
                }
                else
                {
                    ////编辑
                    //try
                    //{
                    //    rtProvider.UpdateItemInfo(this.CurrentID, imdes.Text.Trim(), imname.Text.Trim(), imfac.SelectedValue.Trim(), immode.Text.Trim(), imvnam.Text.Trim(), imium.Text.Trim(), impum.SelectedValue.Trim(), cvr, imtyp.SelectedValue.Trim(), ukg, usiz, max, min, sct, nct, nct, imabc.SelectedValue.Trim(), imbpc.Text.Trim(), imkpc.Text.Trim(), ltm, "0", DateTime.Now, DateTime.Now, LocalGlobal.CurrentUserID);
                    //}
                    //catch (Exception error)
                    //{
                    //    this.ShowErrorMessage(this.GetGlobalResourceString("UpdateErrorMessage") + error.Message);
                    //    return;
                    //}
                }
            }
            //回调
            this.DialogCallback("'CloseRefresh'", "window");
        }

        #endregion

    }
}